Skip to content

Conversation

AbishekRajVG
Copy link
Contributor

This PR tracks the changes as per suggestions from @mostafajahanifar in PR #635

Suggestion 1
To follow the usual convention of moving model first and then using DataParallelism, I would suggest improving this function like below:

def model_to(model: torch.nn.Module, device: str = "cpu") -> torch.nn.Module:
    """Transfers model to cpu/gpu.
    Args:
        model (torch.nn.Module):
            PyTorch defined model.
        device (str):
            Transfers model to the specified device. Default is "cpu".
    Returns:
        torch.nn.Module:
            The model after being moved to cpu/gpu.
    """
    device = torch.device(device)
    model = model.to(device)
    
    # If target device is CUDA and more than one GPU is available, use DataParallel
    if device.type == "cuda" and torch.cuda.device_count() > 1:
        model = torch.nn.DataParallel(model)

    return model

This will also avoid unnecessary overhead of DataParallel is there is only one GPU available.

Again, this can be integrated as a method into the ModelABC class. I mean, it should already has to method inherited from nn.Module. However, if we need torch.nn.DataParallel, we can replace that to method with this one. Then users can call: my_model.to(device)

Suggestion 2
why not move this function into the ModelABC class as a method? So, users can load model weights for our models just like they do with normal Pytorch models? is it possible something like below:

my_model.load_weights_from_path(path) or my_model.load(path)

I assume because ModelABC is inheriting from nn.module, it should already have load_state_dict method.

shaneahmed added 30 commits June 8, 2023 18:10
- Refactor base code from IOSegmentorConfig to IOConfigABC
- No need for a separate function
- Enable git workflow on this PR
- Add missing variable
- Move `to_baseline` code to ABC
- Move `to_baseline` code to ABC
- Fix incorrect calculations for output_resolutions
- Update `PatchPredictor` to use `ModelIOConfigABC`

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Define `ModelIOConfigABC` as a dataclass

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Remove kwargs.
- Define dataclass attributes.

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Add `IOInstanceSegmentorConfig`

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Update yaml for  `IOInstanceSegmentorConfig`

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Update yaml for  `IOInstanceSegmentorConfig`

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Update `to_baseline` for  `IOInstanceSegmentorConfig`

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Update  IOInstanceSegmentorConfig in test_nucleus_instance_segmentor.py

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Update cli for IOInstanceSegmentorConfig

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Remove unnecessary test after kwargs removal

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Move ioconfigs to io_config.py

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Fix circular import

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Refactor to reduce code in inheriting class

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Fix deepsource errors

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Fix ioconfig import to avoid circular imports

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Fix missing input arguments

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Improve error catching

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Default configuration is not correct

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Fix error check

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Fix typo

Signed-off-by: Shan E Ahmed Raza <[email protected]>
- Fix unnecessary update

Signed-off-by: Shan E Ahmed Raza <[email protected]>
pre-commit-ci bot and others added 20 commits October 18, 2023 09:07
…-redefine-patchpredictor

# Conflicts:
#	tests/test_utils.py
#	tiatoolbox/utils/misc.py
@AbishekRajVG AbishekRajVG changed the base branch from develop to dev-define-engines-abc October 26, 2023 15:14
@codecov
Copy link

codecov bot commented Oct 26, 2023

Codecov Report

Merging #728 (f53a75b) into dev-define-engines-abc (d84ab45) will decrease coverage by 11.42%.
Report is 146 commits behind head on dev-define-engines-abc.
The diff coverage is n/a.

@@                     Coverage Diff                     @@
##           dev-define-engines-abc     #728       +/-   ##
===========================================================
- Coverage                   99.77%   88.36%   -11.42%     
===========================================================
  Files                          63       67        +4     
  Lines                        6784     7656      +872     
  Branches                     1352     1491      +139     
===========================================================
- Hits                         6769     6765        -4     
- Misses                          7      874      +867     
- Partials                        8       17        +9     
Files Coverage Δ
tiatoolbox/__init__.py 100.00% <ø> (ø)
tiatoolbox/annotation/__init__.py 100.00% <ø> (ø)
tiatoolbox/annotation/dsl.py 100.00% <ø> (ø)
tiatoolbox/annotation/storage.py 99.68% <ø> (+0.04%) ⬆️
tiatoolbox/cli/__init__.py 100.00% <ø> (ø)
tiatoolbox/cli/common.py 79.68% <ø> (-20.32%) ⬇️
tiatoolbox/cli/nucleus_instance_segment.py 73.07% <ø> (-26.93%) ⬇️
tiatoolbox/cli/patch_predictor.py 77.77% <ø> (-22.23%) ⬇️
tiatoolbox/cli/read_bounds.py 100.00% <ø> (ø)
tiatoolbox/cli/save_tiles.py 100.00% <ø> (ø)
... and 54 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@AbishekRajVG AbishekRajVG added the enhancement New feature or request label Oct 27, 2023
@AbishekRajVG AbishekRajVG marked this pull request as draft October 27, 2023 08:44
@AbishekRajVG AbishekRajVG self-assigned this Oct 27, 2023
@shaneahmed
Copy link
Member

Closing in favour of #733

@shaneahmed shaneahmed closed this Nov 8, 2023
@shaneahmed shaneahmed deleted the dev-update-modelabc branch November 8, 2023 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants